#ifndef __GLOBALS_H_INCLUDED
#define __GLOBALS_H_INCLUDED

#include <scos.h>
#include <string.h>
#include <log.h>

#include "scu.h"
#include "sc.h"

#ifdef __cplusplus
extern "C" {
#endif

#define SC_VAR_STREAM    0
#define SC_VAR_INTEGER   1
#define SC_VAR_DOUBLE    2
#define SC_VAR_POINTER   3

#define SC_INTENT_IN     0
#define SC_INTENT_OUT    1

#define SC_PROC_ILP      0
#define SC_PROC_XTL      1

#define SC_DIST_REGULAR  0
#define SC_DIST_BULK     1

#define SC_EVL_FLOW      0
#define SC_EVL_UNRELATED 1
#define SC_EVL_DEPENDENT 2

#define SC_GRAPH_UNCONNECTED 0
#define SC_GRAPH_TIG       1
#define SC_GRAPH_TDG       2
#define SC_GRAPH_DTIG      3

#define SC_MAP_GREEDY    0
#define SC_MAP_COMBINATORIAL 1

extern int SC_DEFAULT_BUFFER;
extern int SC_FRAME_BUFFER;

struct scc_msg_s;
typedef struct sc_proc_s
{
  int id, bufsize, type;
  char *name;
  struct sockaddr address;
  int addrlen;
  SOCKET fd;

  table_t operations;             /* sc_op_t, referenced */
  table_t buffers;                /* sc_buf_t, referenced */
  void (*resultcallback)(struct sc_proc_s *, struct scc_msg_s *);
} sc_proc_t;

typedef struct sc_op_s
{
  int id, dispatched, finished, parent, parent_condition;
  int type, line, affinity;
  char *text, *format;

  table_t arguments;              /* sc_arg_t, owned */

  struct sc_proc_s *processor;
} sc_op_t;

typedef struct sc_origin_s
{
  int id, isVirtual;
  table_t references;             /* sc_originref_t, owned */
} sc_origin_t;

typedef struct sc_originref_s
{
  int id;
  sc_origin_t *origin;
  struct sc_var_s *variable;
  int delay;
} sc_originref_t;

typedef struct sc_transport_s
{
  sc_proc_t *source, *destination;
  int weight;
} sc_transport_t;

typedef struct sc_var_s
{
  int id, type, valid, flowsize, size, pentagram;

  void *data;
  table_t buffers;                /* sc_buf_t, referenced */

  struct sc_arg_s *producer;
  table_t consumers;              /* sc_arg_t, referenced */
  table_t instances;              /* sc_instance_t, owned */
  table_t originrefs;             /* sc_originref_t, referenced */
  table_t transports;             /* sc_transport_t, owned */

  scos_mutex_t lock;
  scos_cond_t condition;
} sc_var_t;

typedef struct sc_temp_s
{
  int id;
  void *address;
} sc_temp_t;

typedef struct sc_buf_s
{
  int id, unlocked, destroyed, size, file;
  struct sc_var_s *stream;
  struct sc_proc_s *processor;
} sc_buf_t;

typedef struct sc_arg_s
{
  int id, intent, dispatched, buffer;

  struct sc_op_s *operation;
  struct sc_instance_s *instance;
} sc_arg_t;

typedef struct sc_thread_s
{
  int id, blocked, locks;

  struct sc_instance_s *instance;
} sc_thread_t;

typedef struct sc_argdecode_s
{
  int  type, intent;

  void *address;
  int  size;
} sc_argdecode_t;

typedef struct sc_cusparam_s
{
  char *text;
#ifdef __cplusplus
  void (*op)(...);
#else
  void (*op)();
#endif
  table_t args;  
} sc_cusparam_t;

typedef struct sc_settings_s
{
  int argc;
  char **argv;

  int  master;
  char *name;
  char *machinesfile;
  char *benchfile, *outbenchfile;
  char *avfile;
  char *tracefile, *intracefile;
  int  writetodisk;
  char *simulate;
  char *reportfile;
  int  spawn;
  int  hardaffinity;
  float benchmarknoise;
  int multiprocessing;
  int noresources;

  int multiuse;
} sc_settings_t;

extern table_t sc_procs;
extern table_t sc_ops;
extern table_t sc_vars;
extern table_t sc_bufs;
extern table_t sc_threads;

extern log_default_streams_t scl;

extern sc_settings_t settings;
extern double last_makespan_prediction;
extern double last_energy_prediction;

#ifdef __cplusplus
}
#endif

#endif /* __GLOBALS_H_INCLUDED */
